package org.sendvox.freswitch.core.impl;

import org.jboss.netty.channel.Channel;
import org.sendvox.freswitch.core.Application;
import org.sendvox.freswitch.core.CommandException;
import org.sendvox.freswitch.core.SendMsg.CallCommand;
import org.sendvox.freswitch.core.SpeakCommand;
import org.sendvox.freswitch.outbound.OutboundClient;
import org.sendvox.freswitch.util.Arguments;

public class SpeakCommandImpl implements SpeakCommand {

	private String engine;
	private String voice;
	private String text;
	private OutboundClient outboundClient;

	private static final String SEPARATOR = "|";

	public SpeakCommandImpl(OutboundClient outboundClient) {
		this.outboundClient = outboundClient;
	}

	@Override
	public SpeakCommand engine(String engine) {
		Arguments.INSTANCE.validate(engine).isNotNull().isNotEmpty();
		this.engine = engine;
		return this;
	}

	@Override
	public SpeakCommand voice(String voice) {
		Arguments.INSTANCE.validate(voice).isNotNull().isNotEmpty();
		this.voice = voice;
		return this;
	}

	@Override
	public SpeakCommand text(String text) {
		Arguments.INSTANCE.validate(text).isNotNull().isNotEmpty();
		this.text = text;
		return this;
	}

	@Override
	public SpeakCommand execute(Channel channel) throws CommandException {
		Arguments.INSTANCE.validate(channel).isNotNull();
		outboundClient.sendmsg
				.callCommand(CallCommand.EXECUTE)
				.executeAppName(Application.SPEAK)
				.executeAppArg(
						engine.concat(SEPARATOR).concat(voice)
								.concat(SEPARATOR).concat(text))
				.eventLock(true).execute(channel);
		return this;
	}

}
